package com.zy.mc_module.mvp.presenter;

import android.content.Context;
import com.zy.mc_module.mvp.base.BasePresenter;
import com.zy.mc_module.mvp.bean.BaseBean;
import com.zy.mc_module.mvp.contract.SetPwdContract.View;
import com.zy.mc_module.mvp.model.SetPwdModel;
import com.zy.mc_module.utils.StringsUtils;
import com.zy.mc_module.utils.ToastUtil;
import com.zy.mc_module.utils.retrofit.RxObserver;
import com.zy.mc_module.utils.retrofit.RxTransform;
import com.zy.mc_module.utils.retrofit.error.ThdException;

import okhttp3.ResponseBody;

/**
 * Created on 18/12/1
 */
public class SetPwdPresenter extends BasePresenter<View> {

    SetPwdModel pwdModel;

    public SetPwdPresenter() {
        pwdModel = new SetPwdModel();
    }

    public void setPwd(Context context,String userName, String pwd, String repwd, String tel, String code, String incode, String
            openid, String uuid, String nickname, String headimgurl) {
        if (StringsUtils.isNumber(userName)) {
            ToastUtil.showShort("用户名不可为纯数字");
            return;
        }
        if (!StringsUtils.isEmpty(userName) && !StringsUtils.checkUserName(userName)) {
            ToastUtil.showShort("用户名格式错误，用户名仅支持大小写字母、数字及“_”");
            return;
        }
        if (pwd.length() <= 0) {
            ToastUtil.showShort("请输入密码");
            return;
        }
        if (!StringsUtils.checkPwd(pwd)) {
            ToastUtil.showShort("请输入6~16位字母和数字组成的密码");
            return;
        }
        if (!pwd.equals(repwd)) {
            ToastUtil.showShort("两次输入的密码不一致");
            return;
        }
        if (tel.isEmpty() || code.isEmpty()) {
            return;
        }
        pwdModel.setPwd(tel, code, pwd, incode, openid, uuid, nickname, headimgurl, repwd, userName)
                .compose(RxTransform.schedule())
                .subscribe(new RxObserver<ResponseBody>(this, context, "正在登录...") {
                    @Override
                    protected void onBaseNext(ResponseBody beanBaseBean) {

                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                    }
                });
    }

    public void H5SetPassword(Context context,String userName, String pwd, String repwd) {
        if (StringsUtils.isNumber(userName)) {
            ToastUtil.showShort("用户名不可为纯数字");
            return;
        }
        if (!StringsUtils.isEmpty(userName) && !StringsUtils.checkUserName(userName)) {
            ToastUtil.showShort("用户名格式错误，用户名仅支持大小写字母、数字及“_”");
            return;
        }
        if (pwd.length() <= 0) {
            ToastUtil.showShort("请输入密码");
            return;
        }
        if (!StringsUtils.checkPwd(pwd)) {
            ToastUtil.showShort("请输入6~16位字母和数字组成的密码");
            return;
        }
        if (!pwd.equals(repwd)) {
            ToastUtil.showShort("两次输入的密码不一致");
            return;
        }
        pwdModel.H5SetPassword(repwd, userName)
                .compose(RxTransform.schedule())
                .subscribe(new RxObserver<BaseBean<Boolean>>(this, context, "正在登录...") {
                    @Override
                    protected void onBaseNext(BaseBean<Boolean> beanBaseBean) {
                        getView().onSuccess(beanBaseBean.data, 0);
                    }

                    @Override
                    protected void onBaseError(ThdException e) {
                        getView().onError(e);
                    }
                });
    }
}
